package com.nervenets.template.scheduler;

import com.nervenets.general.aspect.RedisLocker;
import com.nervenets.general.utils.JodaUtils;
import com.nervenets.template.service.DataOfGoldService;
import com.nervenets.template.service.DataOfMoneyService;
import com.nervenets.template.service.DataOfUserService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;

@Slf4j
@Component
@EnableScheduling
public class Task {
    @Resource
    private DataOfUserService dataOfUserService;
    @Resource
    private DataOfMoneyService dataOfMoneyService;
    @Resource
    private DataOfGoldService dataOfGoldService;

    /**
     * 每3秒钟执行检查挑战是否完成
     */
    @Scheduled(cron = "*/3 * * * * *")
    @RedisLocker(lockKey = "SchedulerTask.every3SecondsExecute", holdTime = 2)
    public void every3SecondsExecute() {
//        final long start = JodaUtils.getTimestamp();
//        System.out.print("SchedulerTask every3SecondsExecute start " + JodaUtils.parseDate(start));
//
//
//        System.out.println(", execute finished, token " + (JodaUtils.getTimestamp() - start) + "ms");
    }

    /**
     * 每18分钟执行
     */
    @Scheduled(cron = "30 */18 * * * *")
    @RedisLocker(lockKey = "SchedulerTask.every18MinutesExecute", holdTime = 20)
    public void every18MinutesExecute() {
        final long start = JodaUtils.getTimestamp();
        System.out.print("SchedulerTask every18MinutesExecute start " + JodaUtils.parseDate(start));


        System.out.println(", execute finished, token " + (JodaUtils.getTimestamp() - start) + "ms");
    }


    /**
     * 每小时执行
     */
    @Scheduled(cron = "30 0 * * * *")
    @RedisLocker(lockKey = "SchedulerTask.everyHourExecute", holdTime = 3000)
    public void everyHourExecute() {
        System.out.println("SchedulerTask.everyHourExecute execute...");
        long startOfLastHour = JodaUtils.getStartOfLastHour();

    }

    /**
     * 每天执行
     */
    @RedisLocker(lockKey = "SchedulerTask.everyDayExecute", holdTime = 7200)
    @Scheduled(cron = "0 0 1 ? * *")
    public void everyDayExecute() {
        System.out.println("SchedulerTask.everyDayExecute execute...");
        final long startOfYesterday = JodaUtils.getStartOfYesterday();

        //生成明天的基础统计数据
        final long tomorrow = JodaUtils.getEndOfToday() + 1;
        dataOfUserService.checkAndGenerateData(tomorrow);
        dataOfMoneyService.checkAndGenerateData(tomorrow);
        dataOfGoldService.checkAndGenerateData(tomorrow);
    }

    /**
     * 每周执行
     */
    @RedisLocker(lockKey = "SchedulerTask.everyWeekExecute", holdTime = 7200)
    @Scheduled(cron = "0 0 1 ? * MON")
    public void everyWeekExecute() {
        System.out.println("SchedulerTask.everyWeekExecute execute...");

    }


    /**
     * 每月执行一次
     */
    @RedisLocker(lockKey = "SchedulerTask.everyMonthExecute", holdTime = 7200)
    @Scheduled(cron = "0 0 1 1 * *")
    public void everyMonthExecute() {
        System.out.println("SchedulerTask.everyMonthExecute execute...");

    }

}
